#include "workflow.h"
#include "worker.h"
#include "vector.h"
#include <iostream>

Job::~Job() {
    //TODO
    delete worker;//yu
    delete prev;//yu
    delete next;//yu
}

Workflow::Workflow() {
    //TODO
    size=0;//yu
    // head=new Job();
    // tail=new Job();
    head=NULL;//yu
    tail=NULL;//yu
}

Workflow::~Workflow() {
    // Job *this_job;
    if(size==0)return;
    while(1)
    {
        Job *this_job=pop();
        if(size==1)break;
        free(this_job);
    }
    free(head);
    
    
}

int Workflow::insert(Job *j) {
    //TODO
    if(j==NULL)return 1;//yu
    if(head==NULL)
    {
        head=j;
        tail=j;
        j->next=NULL;
        j->prev=NULL;
        size++;
        return 0;
    }
    tail->next=j;//yu
    j->prev=tail;//yu
    j->next=NULL;
    tail=j;//yu
    size++;//yu
    return 0;
}

int Workflow::swap(int original_index, int target_index) {

    int min=(original_index<=target_index)?original_index:target_index;
    int max=(original_index>target_index)?original_index:target_index;
    original_index=min;
    target_index=max;
    if (original_index < 0 || original_index >= size || target_index < 0 || target_index >= size)
        return 1;
    if(original_index==target_index)return 1;
    Job *a;
    Job *b;
    Job *p = head;
    Job *a_pre;
    Job *a_next;
    Job *b_pre;
    Job *b_next;
    for (int i = 0; i < size; i++)
    {
        if (i == original_index)
            a = p;
        if (i == target_index)
            b = p;
        p = p->next;
    }
    if(original_index==0&&target_index==size-1&&target_index-original_index>1)
    //左是头且右是尾且不相邻
    {
        a_next=a->next;
        b_pre=b->prev;
        //断链
        a->next=NULL;
        a_next->prev=NULL;
        b_pre->next=NULL;
        b->prev=NULL;
        //重连
        head=b;
        b->next=a_next;
        a_next->prev=b;
        tail=a;
        a->prev=b_pre;
        b->prev->next=a;
        return 0;
    }
    if (original_index == 0 && target_index == size - 1 && target_index - original_index ==1)
    //左头右尾且相邻
    {
        //断链
        a->next = NULL;
        b->prev = NULL;
        //重连
        head=b;
        tail=a;
        b->next=a;
        a->prev=b;
    }    
    if (original_index == 0 && target_index != size - 1&&target_index-original_index>1) 
    //左是头，右不是尾，不相邻
    {
        a_next = a->next;
        b_pre = b->prev;
        b_next=b->next;
        //断链
        a->next = NULL;
        a_next->prev = NULL;
        b_pre->next = NULL;
        b->prev = NULL;
        b->next=NULL;
        b_next->prev=NULL;
        //重连
        head=b;
        b->next=a_next;
        a_next->prev=b;
        a->prev=b_pre;
        b_pre->next=a;
        a->next=b_next;
        b_next->prev=a;
    }
    if (original_index == 0 && target_index != size - 1 && target_index - original_index == 1)
    //左是头，右不是尾，相邻
    {
        b_next = b->next;
        //断链
        a->next = NULL;
        b->prev = NULL;
        b->next = NULL;
        b_next->prev = NULL;
        //重连
        head = b;
        b->next = a;
        a->prev=b;
        a->next=b_next;
        b->next=a;
    }
    if (original_index != 0 && target_index == size - 1&&target_index-original_index>1)
    //左不是头，右是尾，不相邻
    {
        a_next = a->next;
        b_pre = b->prev;
        a_pre=a->prev;
        //断链
        a->next = NULL;
        a_next->prev = NULL;
        b_pre->next = NULL;
        b->prev = NULL;
        a->prev=NULL;
        a_pre->next=NULL;
        //重连
        b->next = a_next;
        a_next->prev = b;
        b->prev=a_pre;
        a_pre->next=b;
        tail=a;
        a->prev=b_pre;
        b_pre->next=a;
    }
    if (original_index != 0 && target_index == size - 1 && target_index - original_index == 1)
    //左不是头，右是尾，相邻
    {
        a_pre = a->prev;
        //断链
        a->next = NULL;
        b->prev = NULL;
        a->prev = NULL;
        a_pre->next = NULL;
        //重连
        b->prev=a_pre;
        a_pre->next=b;
        b->next=a;
        a->prev=b;
        tail=a;
    }
    if (original_index != 0 && target_index != size - 1 && target_index - original_index > 1)
    //左不是头，右不是尾，不相邻
    {
        a_pre = a->prev;
        a_next=a->next;
        b_pre=b->prev;
        b_next=b->next;
        //断链
        a->prev=NULL;
        a_pre->next=NULL;
        a->next = NULL;
        a_next->prev=NULL;
        b->prev = NULL;
        b_pre->next=NULL;
        b->next=NULL;
        b_next->prev=NULL;
        //重连
        b->prev = a_pre;
        a_pre->next=b;
        b->next=a_next;
        a_next->prev=b;
        a->prev=b_pre;
        b_pre->next=a;
        a->next=b_next;
        b_next->prev=a;
    }
    if (original_index != 0 && target_index != size - 1 && target_index - original_index == 1)
    //左不是头，右不是尾，相邻
    {
        a_pre = a->prev;
        b_next = b->next;
        //断链
        a->prev = NULL;
        a_pre->next = NULL;
        a->next = NULL;
        b->prev = NULL;
        b->next = NULL;
        b_next->prev = NULL;
        //重连
        b->prev = a_pre;
        a_pre->next = b;
        b->next = a;
        a->prev = b;
        a->next = b_next;
        b_next->prev = a;
    }
    return 0;
}

Job *Workflow::pop() {
    //TODO
    if(size==0)
    {
        head=NULL;
        return NULL;
    }
    if(size==1)
    {
        Job *this_head = head;
        head=NULL;
        tail=NULL;
        size--;
        return this_head;
    }
    Job *this_head=head;
    head=head->next;
    head->prev=NULL;
    size--;
    return this_head;
    return NULL;
}

int Workflow::process(vector *l, int index) {
    //TODO
    //
    if(index>=size||index<0)return 1;
    if(l->get_len()==0)return 1;
    Worker* first_worker=l->remove(0);
    Job * this_job;
    int i=0;
    for(this_job=head;head!=NULL;i++)
    {
        if(index==i)break;
        this_job = this_job->next;
    }
    if(this_job->worker==NULL)this_job->worker=first_worker;
    else
    {
        int i;
        for(i=0;i<l->get_len();i++)
        {
            if(this_job->worker==l->get(i))break;
        }
        if (i == l->get_len())
            this_job->worker = first_worker;
        else
        {
            l->remove(i);
            this_job->worker=first_worker;
        }
    }
    
    return 0;
}

/*预警：以下内容不许修改。*/
int Job::num_job = 0;   //Do not remove or modify this line.
//警告：你不可以改动这一行！
Job::Job() {               //警告：你不可以改动这个函数！
    id = num_job;
    num_job++;
    next = NULL;
    prev = NULL;
    worker = NULL;
}
int Workflow::print() {     //警告：你不可以改动这个函数！
    Job * curr = head;
    int i = 0;
    if (curr == NULL) {
        std::cout << "Empty Flow\n";
        return 0;
    }
    while (curr != NULL) {
        std::cout << "[" << i << "]";
        curr->print();
        i++;
        curr = curr->next;
    }
    return 0;
}
int Job::print() {
    std::cout << "job [" << this->id << "]\n";
    return 0;
}

int Workflow::print_reverse()
{ 
    Job *curr = tail;
    int i = 0;
    if (curr == NULL)
    {
        std::cout << "Empty Flow\n";
        return 0;
    }
    while (curr != NULL)
    {
        std::cout << "[" << i << "]";
        curr->print();
        std::string t;
        if (curr->worker == NULL)
        {
            t =""; 
        }
        else
        {
            t=curr->worker->get_name();
        }
        std::cout<<t<<std::endl;
        i++;
        curr = curr->prev;
    }
    return 0;
}
